class Person {
  uname: string;
  age: number;
  un: string;

  constructor(age: number = 50, un: string = "北大", uname?: string) {
    if (uname !== undefined) {
      this.uname = uname;
    }
    this.age = age;
  }

  sayhi() {}
}

const lsper = new Person();
console.log(lsper);

// ! 类的继承
class animl {
  name: string;
  age: number;
  //构造函数加上protect 不能初始化

  protected constructor(name: string, age: number) {
    this.age = age;
    this.name = name;
  }

  sayhi(str: string): void {
    console.log("hi" + str);
  }
}

class Dog extends animl {
  constructor(name: string, age: number) {
    super(name, age);
  }

  sayhi() {
    console.log("我是子类的方法");
    super.sayhi("泰迪");
  }
}
// const animal=new animl()  //构造函数加上protect 不能初始化

const taidi = new Dog("泰迪", 15);
taidi.sayhi();

// 存取器 get和set方法
class Name {
  lastName?: string;
  private firstName: string;

  constructor(firstName: string, lastName?: string) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  // 设置存取器
  get fullName(): string {
    if (this.lastName !== undefined) {
      return `姓: ${this.firstName} 名:${this.lastName}`;
    } else {
      return `姓: ${this.firstName} `;
    }
  }

  set fullName(value: string) {
    this.lastName = value;
  }
}

const zkss = new Name("张", "顺不");
console.log(zkss.fullName);
zkss.fullName = "可以";
console.log(zkss.fullName);

// // ^ 索引签名
class MyClass {
  //? 用于约束成员变量和函数变量
  [s: string]: boolean | ((s: string) => boolean);

  name: boolean;
  // !  name:string; 报错原因是只能定义为boolean
  // 报错参数为stirng
  // check(se: number) {
  //   return se < 10 ? false : true;
  // }
  check(se: string) {
    return se.length < 10 ? false : true;
  }
}

// const classmy = new MyClass();
// classmy.check("a");

// 类中的readobly

// 如果初始化的过程中赋值代表了这个值变为常量了
class zz {
  constructor(readonly name = "sada") {
    //相当于 readonle name:string
    //         this.name=name
    if (name) {
      this.name = name;
    }
  }
}

class zz {
  // readonly name="asdas"
  readonly name: string = "asdas"; //不会报错

  constructor(name: string) {
    if (name) {
      this.name = name; //常量赋值
    }
  }
}

// | 区别点         | 构造参数加 `readonly` | 类属性加 `readonly` |
// | ----------- | ---------------- | --------------- |
// | 是否是类属性      | ✅ 是              | ✅ 是             |
// | 初始化方式       | 自动初始化            | 手动初始化           |
// | 构造函数中可否重新赋值 | ✅ 可以             | ❌ 不可以（只允许一次）    |
// | 推荐程度        | ⭐⭐ 快速建模用         | ⭐⭐⭐⭐ 推荐生产使用     |

const zzkse = new zz("Fdsf");

// zzkse.name='das'  //会报错

class MyClasss {
  [s: string]: (num1: number, uname: string) => string;

  sayhi(x: number, y: string) {
    return x + y;
  }
  sadfsdfdsf(num1: number, uname: string) {
    return uname;
  } // 注意在类中如果使用:方式需要指定函数  不支持function 完整的写法
  zks: (num1: number, uname: string) => string = (
    num1: number,
    uname: string
  ) => {
    return "zks";
  };
}

class point {
  x: number;
  y: number;
}

class point3D {
  x: number;
  y: number;
  z: number;
}

const es: point = new point3D();
// const ses:point3D=new point()
